package com.training1.core.web.controller;


import java.util.Date;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.global.Global;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.common.utils.security.MD5;
import com.training1.core.entity.User;
import com.training1.core.modules.service.UserTestService;


@Controller
@RequestMapping("user")
public class UserController extends BaseController{

	@Resource
	private UserTestService userTestService; 
	
	/**
	 * 列表页面
	 * @param model 往页面传值
	 * @param usernameLike 模糊查询用户名
	 * @param gender 性别
	 * @param page 分页
	 * @return
	 */
	@RequestMapping({"/",""})
	public String users(){
//		page.getSearch().put("usernameLike", usernameLike);
//		page.getSearch().put("gender", gender);
//		userTestService.findList(page.getSearch());
//		model.put(Page.PAGEKEY, page);
		return "user/userList2";
	}
	
	/**
	 * 列表页面
	 * @param model 往页面传值
	 * @param usernameLike 模糊查询用户名
	 * @param gender 性别
	 * @param page 分页
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody
	public Page<User> list(String usernameLike,
			Integer gender,
			Date birthdayFrom,
			Date birthdayTo,
			Page<User> page){
		page.getSearch().put("usernameLike", usernameLike);
		page.getSearch().put("gender", gender);
		page.getSearch().put("birthdayFrom", birthdayFrom);
		page.getSearch().put("birthdayTo",birthdayTo);
		userTestService.findList(page.getSearch());
		return page;
	}
	
	/**
	 * 新增编辑页面
	 * @param model
	 * @param id 如果存在，则为编辑
	 * @return
	 */
	@RequestMapping("edit")
	@ResponseBody
	public User edit(Integer id){
			User user = userTestService.get(id);
		return user; 
	}
	
	/**
	 * 新增编辑页面
	 * @param model
	 * @param id 如果存在，则为编辑
	 * @return
	 */
//	@RequestMapping("edit")
	@Deprecated
	public String edit1(ModelMap model,Integer id){
		if(id!=null){
			User user = userTestService.get(id);
			model.put("user", user);
		}
		return "user/userEdit";
	}
	
	/**
	 * 通过ajax保存
	 * @param user
	 * @param result 
	 * @return
	 */
	@RequestMapping(value="save", method={RequestMethod.POST})
	@ResponseBody
	public RespJson save(@Valid User user,BindingResult result ){
		if(result.hasErrors()){
			return RespJsonFactory.buildFailure(result.getAllErrors().get(0).getDefaultMessage(), "module.user.save.error");
		}
		if(user.getId()==null){
			//判断用户是否存在
			if(userTestService.isUserExist(user)){
				return RespJsonFactory.buildFailure("module.user.exist");
			}
			//密码无效
//			if(StringUtils.isBlank(user.getPassword())){
//				return RespJsonFactory.buildFailure("module.user.password.error");
//			}
			user.setPassword(MD5.getMD5(Global.getConfig("user.password.default")));
		}else{
			//不改变用户名和密码
//			user.setUsername(null);
			user.setPassword(null);
		}
		userTestService.save(user);
		
		return RespJsonFactory.buildSuccess();
	}
	
	@RequestMapping(value="delete", method={RequestMethod.DELETE})
	@ResponseBody
	public RespJson delete(@RequestBody User user ){
		
		userTestService.delete(user.getId());
		return RespJsonFactory.buildSuccess();
	}
}
